package com.tianji.remark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.remark.domain.dto.LikeRecordFormDTO;
import com.tianji.remark.domain.po.LikedRecord;
import com.tianji.remark.mapper.LikedRecordMapper;
import com.tianji.remark.service.ILikedRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.tianji.common.constants.MqConstants.Exchange.LIKE_RECORD_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author leiyuhang
 * @since 2023-07-28
 */
@Service
@RequiredArgsConstructor
public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {

    private final RabbitMqHelper mqHelper;
    @Override
    public void addLikeRecord(LikeRecordFormDTO recordDTO) {
        //1.基于前端函数判断点赞还是删除
        boolean success =  recordDTO.getLiked()?like(recordDTO):unlike(recordDTO);
        //2.执行失败则直接返回
        if (!success){
            return;
        }
        //3.执行成功则执行点赞数
        Integer count = lambdaQuery().eq(LikedRecord::getBizId, recordDTO.getBizId()).count();
        //4.发送mq消息
        mqHelper.send(
                LIKE_RECORD_EXCHANGE,
                StringUtils.format(LIKED_TIMES_KEY_TEMPLATE, recordDTO.getBizType()),
                new LikedTimesDTO(recordDTO.getBizId(), count));
    }

    @Override
    public Set<Long> isBizLiked(List<Long> bizIds) {
        Long user = UserContext.getUser();
        List<LikedRecord> list = lambdaQuery()
                .eq(LikedRecord::getUserId, user)
                .in(LikedRecord::getBizId, bizIds)
                .list();

        return list.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
    }

    private boolean unlike(LikeRecordFormDTO recordDTO) {

        return remove(new LambdaQueryWrapper<LikedRecord>().eq(LikedRecord::getUserId, UserContext.getUser())
                .eq(LikedRecord::getBizId, recordDTO.getBizId()));
    }

    private boolean like(LikeRecordFormDTO recordDTO) {
        //1.获取用户id
        Long user = UserContext.getUser();
        //2.查询点赞是否存在
        Integer count = lambdaQuery().eq(LikedRecord::getUserId, user)
                .eq(LikedRecord::getBizId, recordDTO.getBizId())
                .count();
        //3.存在则返回失败
        if (count>0){
            return  false;
        }
        //4.不存在则新增
        LikedRecord likedRecord = BeanUtils.copyBean(recordDTO,LikedRecord.class);
        likedRecord.setUserId(user);
        save(likedRecord);
        return true;
    }
}
